
bt.module({builder: function() {
    var j$ = jQuery;

    var _initLoadMoreEvent = function() {
        j$("#load-more").click(function() {
            var _this = this;
            j$('#ajax-more').css("visibility", "visible");

            j$.ajax({
                url: "/search_trips/",
                data: "start=" + j$(_this).data("start")
                    + "&count=" + j$(_this).data("count")
                    + "&q=" + encodeURI(j$(_this).data("key")),
                success: function(resp) {
                    if(resp) {
                        j$("#trips").append(resp);
                    } else {
                        j$("#load-more").slideUp();
                    }
                }
            }).done(function() {
                j$(_this).remove();
                _initLoadMoreEvent();
                _ellipsisCities();
                _ellipsisTripNames();
            });
        });
    };

    var _initLoadMoreUsersEvent = function() {
        j$("#users-load-more").click(function() {
            var _this = this;
            j$('#users-ajax-more').css("visibility", "visible");

            j$.ajax({
                url: "/search_users/",
                data: "start=" + j$(_this).data("start")
                    + "&count=" + j$(_this).data("count")
                    + "&q=" + encodeURI(j$(_this).data("key")),
                success: function(resp) {
                    if(resp) {
                        console.debug(resp);
                        j$("#users").append(resp);
                    } else {
                        j$("#users-load-more").slideUp();
                    }
                }
            }).done(function() {
                    j$(_this).remove();
                    _initLoadMoreUsersEvent();
                });
        });
    };

    var _ellipsisPlaceNames = function() {
        var ellipsis_settings = {
            max_rows: 1,
            alt_text_t: true,
            whole_word: false
        };
        j$(".place-item .name").each(function() {
            j$(this).ThreeDots(ellipsis_settings);
        });
    };

    var _ellipsisTripNames = function() {
        var ellipsis_settings = {
            max_rows: 1,
            alt_text_t: true,
            whole_word: false
        };
        j$(".trip-item .name").each(function() {
            j$(this).ThreeDots(ellipsis_settings);
        });
    };

    var _ellipsisCities = function() {
        var ellipsis_settings = {
            max_rows: 2,
            valid_delimiters: ['•'],
            alt_text_t: true
        };
        // ellipsis sities
        j$(".trip-item .summary .cities").each(function() {
            j$(this).ThreeDots(ellipsis_settings);
        });
    };

    var _ellipsisHotPanel = function() {
        var ellipsis_opts = {max_rows: 1};
        // hot_cities & hot_countries
        j$(".hot-panel .name a").each(function() {
            j$(this).ThreeDots(ellipsis_opts);
        });

    };

    j$(document).ready(function() {
        j$("#result-panel ul.tabs").tabs("#result-panel .panels>.panel",
            {initialIndex: j$("#result-panel .panels").data("init-index")});
        _initLoadMoreEvent();
        _initLoadMoreUsersEvent();
        // _ellipsisHotPanel();
        // _ellipsisCities();
        // _ellipsisTripNames();
        // _ellipsisPlaceNames();
    });
}});
